package com.hkyl.hospital.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hkyl.hospital.entity.*;
import com.hkyl.hospital.mapper.DeptMapper;
import com.hkyl.hospital.mapper.PatientMapper;
import com.hkyl.hospital.mapper.RegisterMapper;
import com.hkyl.hospital.mapper.UserMapper;
import com.hkyl.hospital.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.ResourceBundle;

/**
 * @author hyh
 * @date 2023/07/12 14:50:20
 */
@Service
@Transactional
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private RegisterMapper registerMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private PatientMapper patientMapper;

    @Override
    public PageInfo<Register> findAll(Map map) {
        Integer pageNum = (Integer) map.get("pageNum");
        Integer pageSize = (Integer) map.get("pageSize");
        Integer userId = (Integer) map.get("userId");
        userId = 1;
        Boolean isToday = (Boolean) map.get("isToday");
        RegisterExample registerExample = new RegisterExample();
        registerExample.or().andDoctorEqualTo(userId);
        if (isToday){
            registerExample = new RegisterExample();
            registerExample.or().andDoctorEqualTo(userId).andRegisterDateEqualTo(new Date());
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Register> registers = registerMapper.selectByExample(registerExample);
        List<Dept> depts = deptMapper.selectByExample(null);
        List<User> doctors = userMapper.selectByExample(null);
        List<Patient> patients = patientMapper.selectByExample(null);

        for (Register register : registers) {
            Dept registerDept = depts.stream().filter(dept -> dept.getDeptId() == register.getDeptId())
                    .collect(Collectors.toList()).get(0);
            User registerDoctor = doctors.stream().filter(doctor -> doctor.getUserId() == register.getDoctor())
                    .collect(Collectors.toList()).get(0);
            Patient registerPatient = patients.stream().filter(patient -> patient.getPatientId() == register.getPatientId())
                    .collect(Collectors.toList()).get(0);
            register.setDept(registerDept);
            register.setPhysician(registerDoctor);
            register.setPatient(registerPatient);
        }
        PageInfo<Register> pageInfo = new PageInfo<>(registers);
        return pageInfo;
    }
}
